//
//  CZTabBar.m
//  YingKeLive
//
//  Created by 西瓜大浪 on 2021/9/2.
//

typedef NS_ENUM(NSUInteger, CZItemType) {
    CZItemTypeLive = 1,
    CZItemTypeMe = 2,
    
};
#import "CZLaunchController.h"
#import "CZTabBar.h"
@interface CZTabBar ()
//中间按钮
@property(nonatomic,strong)UIButton *cameraButton;
@end


@implementation CZTabBar
//懒加载中间按钮
-(UIButton *)plusButton{
    
    if(_cameraButton == nil){
        _cameraButton = [[UIButton alloc]init];
        
        //设置图片
        [_cameraButton setImage:[UIImage imageNamed:@"tab_launch"] forState:UIControlStateNormal];
        //自适应大小
        [_cameraButton sizeToFit];
        //添加到tabbar 不能少
        [self addSubview:_cameraButton];
        //监听点击
        [_cameraButton addTarget:self action:@selector(plusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        //取消高亮状态
        _cameraButton.adjustsImageWhenHighlighted = NO;
        
    }
    return  _cameraButton;
}


-(void)layoutSubviews{
    
    [super layoutSubviews];
    
    
    //获取tabbar上有多少子控制器 +1 表示中间的留一个位置
    NSInteger count = self.items.count +1;
    
    
    CGFloat btnW = SREENWIDTH / count; //分成三份
    CGFloat btnH = 49; //底部tabbar高度
    CGFloat btnX = 0;
    
    
    //索引
    NSInteger i = 0 ;
    
    //遍历子控件  如果是UITabBarButton 就设置位置
    for (UIControl *tabbarbutton in self.subviews) { //UITabBarButton 是私有的 可以用父类UIControl 来接受
        
        //遍历所有子控件 如果是UITabBarButton 就设置位置
        if([tabbarbutton isKindOfClass:NSClassFromString(@"UITabBarButton")]){
            
            //如果为第2个 就跳到下一个 空出来放加号按钮
            if(i ==1){
                
                i+=1;
            }
            
            btnX = btnW * i;
            tabbarbutton.frame = CGRectMake(btnX, 0, btnW,btnH);
            i++;
            
            //监听Tabbar
            [tabbarbutton addTarget:self action:@selector(tabbarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
     
        }
        }
    //设置中间加号按钮的center
    self.plusButton.center = CGPointMake(self.frame.size.width *0.5,49 * 0.5);
        
 
 
    
}
    
    

    



//tabbar的点击事件
-(void)tabbarButtonClick:(UIControl *)button{

    
    UIButton *btn = (UIButton *)button;
    
    [self animationWithButton:btn];
    
}

//中间按钮的点击事件
-(void)plusButtonClick:(UIButton *)sender{
    
    //缩放按钮
    [self animationWithButton:sender];
    
    //直播控制器
    CZLaunchController *launchVc =  [CZLaunchController new];
    //modal样式 全屏幕
    launchVc.modalPresentationStyle = UIModalPresentationFullScreen;
    //modal 跳转
    [self.window.rootViewController presentViewController:launchVc animated:YES completion:nil];
    /**
     当前不是在控制器类的时候 无法使用Push modal pop
     可以调用当前的 rootViewController 来进行modal
     
     */
}



//按钮动画
-(void)animationWithButton:(UIButton *)button{
    
    
    [UIView animateWithDuration:0.1 animations:^{
       
        //通过动画的方式扩大0.25倍
        button.transform = CGAffineTransformMakeScale(1.2, 1.2);
    }completion:^(BOOL finished) {
       //上面的执行完后 再执行下面的
        if(finished){
            //复原
            [UIView animateWithDuration:0.25 animations:^{
                            button.transform = CGAffineTransformIdentity;
            }];
        }
       
    }];
    
    
    
    
}


@end
